package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pojo.User;
import com.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    
    @Resource
    private UserService userService;

    @RequestMapping(value = "/index.html",method = RequestMethod.POST)
    public ModelAndView getUserAll(Map map,Model model, HttpSession session){
       /* Controller ---> View*/
        //model.addAttribute("name","lisi");
        //session.setAttribute("name","lisi");
        //map.put("name","lisi");
        ModelAndView modelAndView= new ModelAndView();
        modelAndView.addObject("name",map);
        return modelAndView;
    }

    @RequestMapping(value ={"/index","/index1"} ,method = RequestMethod.POST)
    public String getUserOn(int id){
        /*@RequestParam(value = "id",required = true)*/
        /*View ----> Controller*/
      /*  String id = request.getParameter("id");
        System.out.println(id);*/
        System.out.println(id);
        return "welcom";
    }

    @RequestMapping(value = "/index2",method = RequestMethod.POST)
    public String index2(User user,Model model){
        List<User> userAll = userService.getUserAll();
        model.addAttribute("userAll",userAll);
        return "welcom";
    }



    @RequestMapping(value = "/index3")
    public String index3(Model model, HttpServletRequest request){
        List<User> userAll = userService.getUserAll();
        model.addAttribute("userAll",userAll);
        int size = userAll.size();
        if(size>0){
            throw  new RuntimeException("测试局部处理异常");
        }else {
            return "login";
        }
    }

    //@RequestMapping("/index4") ///index4/id=1
    /* @RequestMapping("/index4/{id}/{name}") ///index4/id=1/name=lisi
    public String index4(@PathVariable("id") Integer id,@PathVariable("name") String name){

         System.out.println(id);

        return "login";
    }*/

   /* @RequestMapping("/index4/{json}") //index4/id=1/name=lisi
    public String index4(@PathVariable("json") String json){
        User user = JSONObject.parseObject(json, User.class);
        System.out.println(json);
        return "redirect:/index3"; //重定向
    }*/

    /*ajax请求*/
   // @RequestMapping(value = "/index4",produces  ={"application/json;charset=UTF-8"}) //index4/id=1/name=lisi
    @RequestMapping(value = "/index4") //index4/id=1/name=lisi
    @ResponseBody
    public String index4(User user, HttpServletResponse response){
        System.out.println(user);
        User user1= userService.getUser(80);
        String string = JSON.toJSONString(user1);
        return string;
    }


    @RequestMapping("/index5")
    public String index5(User user,
                         @RequestParam("picpath") MultipartFile multipartFile,
                         @RequestParam("picpath1") MultipartFile multipartFile1) throws IOException {

        String path="E:\\备课笔记\\Kgc备课\\Java全部的课程\\13-Spring MVC框架\\3.Spring MVC核心应用-2\\01-教学PPT\\";
        String pa=path+multipartFile.getOriginalFilename();
        System.out.println(multipartFile.getName());
        File file = new File(pa);
        //文件上传
        multipartFile.transferTo(file);
        //持久化
        user.setPicPath(pa);
        int i = userService.addUser(user);
        System.out.println(i);
        System.out.println("1231");
        System.out.println("1231");
        return "index";
    }



    /*异常处理--局部：仅能处理指定Controller中的异常*//*
    @ExceptionHandler(Exception.class)
    public  String  exceptionHandler(HttpSession session,RuntimeException runtime){
        session.setAttribute("runtime",runtime.getMessage());
        return "error";
    }*/

}
